<?php

// 获取商品详细数据
$json = array('status' => 'error', 'msg' => '', 'data' => []);

$id = fn_post('id', fn_get('id'));

if (!$id) {
	$json['msg'] = '错误编号';
} elseif (!$this->website['class']['db']
		->table('product')
		->where('id=? and checked=1', $id)
		->limit(1)
		->count()) {
	$json['msg'] = '未找到或未通过审核';
} else {
	// 商品基本信息
	$json['data']['product'] = $this->website['class']['db']
		->table('product')
		->where('id=?', $id)
		->one();
	// 判断收藏过没
	if (($_POST['basic'] ?? false) && ($basic = json_decode($_POST['basic'], 1))) {
		$json['data']['product']['collect'] = $this->website['class']['db']
			->table('product_collect')
			->where('uid=? and pid=?', $basic['uid'], $json['data']['product']['id'])
			->count();
	} else {
		$json['data']['product']['collect'] = 0;
	}
	// 各类规格及价格
	$json['data']['params'] = $this->website['class']['db']
		->table('product_params')
		->where('id=?', $id)
		->one();
	// 商品价格-折扣价
	$json['data']['prices'] = array(
		'original' => [], //原价
		'discount' => [], //折扣价
	);

	if ($json['data']['product']['pricemin'] && $json['data']['product']['pricemax']) {
		if ($json['data']['product']['pricemin'] !== $json['data']['product']['pricemax']) {
			$json['data']['prices']['original'][] = $json['data']['product']['pricemin'] / 100;
			$json['data']['prices']['original'][] = $json['data']['product']['pricemax'] / 100;
		} else {
			$json['data']['prices']['original'][] = $json['data']['product']['pricemin'] / 100;
		}
	} else {
		$json['data']['prices']['original'][] = $json['data']['product']['price'] / 100;
	}
	if ($json['data']['product']['discount'] && $json['data']['product']['discount'] !== 100) {
		foreach ($json['data']['prices']['original'] as $v) {
			$price = $v * 100 * $json['data']['product']['discount'];
			$json['data']['prices']['discount'][] = bcdiv($price, 10000, 2);
		}
	}

	// $json['data']['price'] = $price;

	// 商品图片
	$json['data']['image'] = $this->website['class']['db']
		->table('product_image')
		->field('image')
		->where('pid=?', $id)
		->select();
	// 商品参数
	$args = $this->website['class']['db']
		->table('product_args')
		->field('args')
		->where('id=?', $id)
		->one();
	$json['data']['args'] = $args['args'] ?? json_encode([]);
	// 商品详细信息
	$json['data']['detail'] = $this->website['class']['db']
		->table('product_detail')
		->field('id', 'num', 'image')
		->where('pid=?', $id)
		->select();
	// 商家信息
	$json['data']['shops'] = $this->website['class']['db']
		->table('shops')
		->where('id=?', $json['data']['product']['uid'])
		->one();

	// 商品评论
	$json['data']['comment'] = $this->website['class']['db']
		->table('product_comment')
		->where('pid=? and checked=1', $id)
		->order('id desc')
		->one();
	if ($json['data']['comment']['id'] ?? false) {
		$commentImages = $this->website['class']['db']
			->table('product_comment_images')
			->where('cid=?', $json['data']['comment']['id'])
			->select();
		$json['data']['comment']['images'] = $commentImages;
		// 评论用户信息
		$json['data']['comment']['user'] = $this->website['class']['db']
			->table('user')
			->field('uname', 'header')
			->where('id=?', $json['data']['comment']['uid'])
			->one();
	}

	// 是否有促销
	if ($json['data']['product']['coupon'] == 1) {
		// 已有优惠券
		$coupon = $this->website['class']['db']
			->table('coupon_user')
			->field('cid')
			->where('uid=?', $json['data']['product']['uid'])
			->select();
		$inArr = array_column($coupon, 'cid');
		$in = '';
		if (count($inArr)) {
			$in = rtrim(str_pad('?', 2 * count(array_column($inArr, 'cid')), ',?'), ',');
		}
		$seArr = array_merge(
			[
				$json['data']['product']['uid'],
				$id, $json['data']['product']['tid'],
				$_SERVER['REQUEST_TIME'],
				$_SERVER['REQUEST_TIME'],
			],
			$inArr
		);

		$json['seArr'] = $inArr;

		// 优惠券
		$json['data']['coupon'] = $this->website['class']['db']
			->table('coupon')
			->where(
				'((sid=? or pid=? or tid=?) or (sid=0 and pid=0 and tid=0)) and endtime>? and starttime<?' . (count($inArr) ? ' and id not in(' . $in . ')' : ''),
				$seArr
			)
			->select();
	} else {
		$json['data']['coupon'] = [];
	}

	// 店内商品推荐
	$recommend = $this->website['class']['db']
		->table('product')
		->field('id', 'tit', 'image', 'price')
		->where('id !=? and uid=? and tid=? and checked=1', $id, $json['data']['product']['uid'], $json['data']['product']['tid'])
		->num(8)
		->order('shopsrecommend desc')
		->select();
	$json['data']['recommend'] = $recommend ? $recommend : [];

	if ($json['data']['product']['uid']) {
		$json['status'] = 'ok';
	}

}

$this->json_encode_encrypt($json);